const { createCanvas, createImageData } = require("canvas");
const LngRange = [-180, 180];

const LatRange = [-85, 85];

const MapLngRange = 360;

const MapLatRange = 170;

const TileSize = 255;

/**
 * @description 将 瓦片xy坐标转换为 QuadKey
 * @param  tileX
 * @param  tileY
 * @param  level
 * @returns
 */
const TileXYToQuadKey = function (tileX, tileY, level) {
  var QuadKey = "";
  for (var i = level; i >= 0; --i) {
    var bitmask = 1 << i;
    var digit = 0;

    if ((tileX & bitmask) !== 0) {
      digit |= 1;
    }

    if ((tileY & bitmask) !== 0) {
      digit |= 2;
    }

    QuadKey += digit;
  }
  return QuadKey;
};

/**
 * @description 将quadkey 转换为 瓦片XY坐标和 层级
 * @param  quadkey
 */
const QuadKeyToTileXY = function (quadkey) {
  var x = 0;
  var y = 0;
  var level = quadkey.length - 1;
  for (var i = level; i >= 0; --i) {
    var bitmask = 1 << i;
    var digit = +quadkey[level - i];

    if ((digit & 1) !== 0) {
      x |= bitmask;
    }

    if ((digit & 2) !== 0) {
      y |= bitmask;
    }
  }
  return {
    x: x,
    y: y,
    level,
  };
};

/**
 * @description 生成灰度图
 * @param {Buffer} data 源数据
 * @param {Number} max
 * @param {Number} min
 */
const GrayscaleGeneration = function (data, max, min,w,h) {
  
  const uint8ImageData = new Uint8ClampedArray(w * h * 4);

  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      const gird_index = y * w + x;
      const _index = gird_index * 4;

      const value = Number.parseFloat(data.readFloatLE(_index).toFixed(5))

      // uint8ImageData[_index + 0] = Math.floor(
      //   (255 * (value - min)) / (max - min)
      // );
      uint8ImageData[_index + 0] = Math.floor((value - min) / (max-min) * 255);
      uint8ImageData[_index + 1] = 0;
      uint8ImageData[_index + 2] = 0;
      uint8ImageData[_index + 3] = 255;
    }
  }

  const imgFrameData = createImageData(uint8ImageData, w, h);
  let revertImageData = createImageData(w,h);
  imageDataVRevert(imgFrameData,revertImageData);

  // return revertImageData;
  const canvas = createCanvas(w, h);
  const ctx = canvas.getContext("2d");
  ctx.clearRect(0, 0, w, h);
  ctx.putImageData(revertImageData, 0, 0, 0, 0, w, h);
  // const base64 = canvas.toDataURL("image/png", 1);
  // return Buffer.from(base64.replace("data:image/png;base64,", ""), "base64");
  data = null;
  return canvas;
};

const  imageDataVRevert =function(sourceData, newData) {
  for (var i = 0, h = sourceData.height; i < h; i++) {
    for (var j = 0, w = sourceData.width; j < w; j++) {
      newData.data[i * w * 4 + j * 4 + 0] =
        sourceData.data[(h - i) * w * 4 + j * 4 + 0];
      newData.data[i * w * 4 + j * 4 + 1] =
        sourceData.data[(h - i) * w * 4 + j * 4 + 1];

        
      newData.data[i * w * 4 + j * 4 + 2] =
        sourceData.data[(h - i) * w * 4 + j * 4 + 2];
      newData.data[i * w * 4 + j * 4 + 3] =
        sourceData.data[(h - i) * w * 4 + j * 4 + 3];
    }
  }
  return newData;
}

module.exports = {
  LngRange,
  LatRange,
  MapLatRange,
  MapLngRange,
  TileSize,
  TileXYToQuadKey,
  QuadKeyToTileXY,
  GrayscaleGeneration,
  imageDataVRevert
};
